# *-* coding: UTF-8 *-*
# 文件名： sendemail.py

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

host = 'smtp.qq.com'
port = 587
user = '394631316@qq.com'
password = 'lgmcyjelcyhibjba'

sender = '394631316@qq.com'
receivers = ['jakli@163.com']

# message = MIMEText('python 邮件发送测试', 'plain', 'utf-8')
message = MIMEMultipart()
message['From'] = Header('<394631316@qq.com>')
message['To'] = Header('jack <jakli@163.com>')

subject = 'python smtp 邮件测试'

message['Subject'] = Header(subject, 'utf-8')

# 邮件正文内容
message.attach(MIMEText('这是python 发送邮件测试...', 'plain', 'utf-8'))

# 构建附件1
att1 = MIMEText(open('test1.txt', 'rb').read(), 'base64', 'utf-8')
att1['Content-Type'] = 'application/octet-stream'

# 这里的filename可以任意写，写什么名字，邮件中显示什么名字
att1['Content-Disposition'] = 'attachement; filename="test.txt"'
message.attach(att1)

# 构建附件2
att2 = MIMEText(open('test2.txt', 'rb').read(), 'base64', 'utf-8')
att2['Content-Type'] = 'application/octet-stream'
att2['Content-Disposition'] = 'attachment; filename="test2.txt"'
message.attach(att2)

print(message.as_string())

try:
    smtpObj = smtplib.SMTP(host=host)
    smtpObj.connect(host, port)
    smtpObj.login(user, password)
    smtpObj.sendmail(sender, receivers, message.as_string())
    print('邮件发送成功')
except smtplib.SMTPException as ex:
    print('Error: 邮件发送失败', ex.args)
